<template>
  <div>
    <!-- 评论列表 -->
    <div class="cmt-list" :class="isShow ? 'art-cmt-container-1' : 'art-cmt-container-2'">
      <!-- 上拉加载更多 -->
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多评论了,请先发表评论"
        @load="onLoad"
        :immediate-check="false"
      >
        <!-- 评论的 Item 项 -->
        <div class="cmt-item" v-for="obj in commentList" :key="obj.com_id">
          <!-- 头部区域 -->
          <div class="cmt-header">
            <!-- 头部左侧 -->
            <div class="cmt-header-left">
              <img :src="obj.aut_photo" alt="" class="avatar" />
              <span class="uname">{{
                obj.aut_name === '黑马程序员(改不了)' || obj.aut_name === '黑马先锋'
                  ? 'Hero'
                  : obj.aut_name
              }}</span>
            </div>
            <!-- 头部右侧 -->
            <div class="cmt-header-right" @click="clickLikeFn(obj.is_liking, obj)">
              <van-icon name="like" size="16" color="red" v-if="obj.is_liking" />
              <van-icon name="like-o" size="16" color="gray" v-else />
            </div>
          </div>
          <!-- 主体区域 -->
          <div class="cmt-body">
            {{ obj.content }}
          </div>
          <!-- 尾部区域 -->
          <div class="cmt-footer">{{ timeAgo(obj.pubdate) }}</div>
        </div>
      </van-list>
    </div>

    <!-- 底部添加评论区域 - 1 -->
    <div class="add-cmt-box van-hairline--top" v-if="isShow">
      <van-icon name="arrow-left" size="0.48rem" @click="$router.back()" />
      <div class="ipt-cmt-div" @click="clickIpt">发表评论</div>
      <div class="icon-box">
        <van-badge>
          <van-icon
            name="comment-o"
            size="0.53333334rem"
            @click="clickScroll"
            :badge="commentCount === 0 ? '' : commentCount"
          />
        </van-badge>
        <div @click="clickCollectFn">
          <van-icon name="star" size="0.53333334rem" color="gold" v-if="collectedArt" />
          <van-icon name="star-o" size="0.53333334rem" v-else />
        </div>
        <van-icon name="share-o" size="0.53333334rem" @click="clickShareFn" />
        <van-share-sheet
          v-model="showShare"
          title="立即分享给好友"
          :options="options"
          @select="onSelect"
          get-container="body"
        />
      </div>
    </div>

    <!-- 底部添加评论区域 - 2 -->
    <div class="cmt-box van-hairline--top" v-else>
      <textarea
        placeholder="友善评论、理性发言、阳光心灵"
        v-focus
        @blur="blur"
        v-model.trim="comText"
      ></textarea>
      <van-button
        :type="comText === '' ? 'default' : 'info'"
        :disabled="comText === '' ? true : false"
        @click.stop="sendComment"
        >发布</van-button
      >
    </div>
  </div>
</template>

<script>
import {
  getCommentsAPI,
  LikeCommentAPI,
  DislikeCommentAPI,
  sendCommentAPI,
  collectArtAPI,
  unCollectArtAPI
} from '@/api'
import { timeAgo } from '@/utils/day.js'
import { Toast } from 'vant'
export default {
  data() {
    return {
      commentList: [], // 评论列表
      isLike: false, // 点赞评论
      isShow: true, // 显示评论框
      commentCount: '', // 评论数
      comText: '', // 评论文本框内容
      loading: true, // 是否加载
      finished: false, // 是否结束
      lastID: null, // 分页
      showShare: false, // 是否显示分享弹出层
      options: [
        [
          { name: '微信', icon: 'wechat' },
          { name: '朋友圈', icon: 'wechat-moments' },
          { name: '微博', icon: 'weibo' },
          { name: 'QQ', icon: 'qq' }
        ],
        [
          { name: '复制链接', icon: 'link' },
          { name: '分享海报', icon: 'poster' },
          { name: '二维码', icon: 'qrcode' },
          { name: '小程序码', icon: 'weapp-qrcode' }
        ]
      ]
    }
  },
  props: {
    collectedArt: {
      type: Boolean,
      default: false
    }
  },
  created() {
    this.getComments()
  },
  methods: {
    timeAgo,
    // 获取评论
    async getComments() {
      const {
        data: { data }
      } = await getCommentsAPI({ article_id: this.$route.query.article_id, offset: this.lastID })
      console.log(data)
      if (data.end_id !== null) {
        this.commentList = [...this.commentList, ...data.results]
        this.commentCount = data.total_count
        this.lastID = data.last_id
      } else {
        this.finished = true
      }
      this.loading = false
    },
    // 点赞评论
    async clickLikeFn(bool, obj) {
      if (bool) {
        // 点击红心
        // 业务->取消点赞
        // 发送请求
        obj.is_liking = false
        const data = DislikeCommentAPI(obj)
        console.log(data)
      } else {
        // 点击白心
        // 业务->点赞
        // 发送请求
        obj.is_liking = true
        const data = await LikeCommentAPI(obj)
        console.log(data)
      }
    },
    // 点击显示评论框
    clickIpt() {
      this.isShow = false
    },
    // 输入框失焦隐藏
    blur() {
      if (this.comText === '') {
        this.isShow = true
      }
    },
    // 点击滚动
    // 比较方便的方法(既能滚动也能带动画)
    // 原生标签.scrollIntoView()，让原生的标签滚动到屏幕的最上面
    clickScroll() {
      document.querySelector('.like-box').scrollIntoView({
        behavior: 'smooth'
      })
    },
    // 发布评论
    async sendComment() {
      this.isShow = true
      const {
        data: { data }
      } = await sendCommentAPI(this.$route.query.article_id, this.comText)
      console.log(data)
      this.commentList.unshift(data.new_obj)
      this.commentCount++
      this.clickScroll()
      this.comText = ''
    },
    // 上拉加载触发的方法（加载更多评论）
    onLoad() {
      this.getComments()
    },
    // 点击收藏文章
    async clickCollectFn() {
      if (this.collectedArt) {
        // 点击黄色收藏
        // 业务->取消收藏
        console.log('黄色')
        this.$emit('changeCollect')
        const data = await unCollectArtAPI(this.$route.query)
        console.log(data)
      } else {
        // 点击白色收藏
        // 业务->收藏
        console.log('白色')
        this.$emit('changeCollect')
        const data = await collectArtAPI(this.$route.query)
        console.log(data)
      }
    },
    // 点击分享文章
    clickShareFn() {
      this.showShare = true
    },
    // 点击分享选项时触发
    onSelect(option) {
      Toast(option.name)
      this.showShare = false
    }
  }
}
</script>

<style scoped lang="less">
.cmt-list {
  padding: 10px;
  .cmt-item {
    padding: 15px 0;
    + .cmt-item {
      border-top: 1px solid #f8f8f8;
    }
    .cmt-header {
      display: flex;
      align-items: center;
      justify-content: space-between;
      .cmt-header-left {
        display: flex;
        align-items: center;
        .avatar {
          width: 40px;
          height: 40px;
          background-color: #f8f8f8;
          border-radius: 50%;
          margin-right: 15px;
        }
        .uname {
          font-size: 12px;
        }
      }
    }
    .cmt-body {
      font-size: 14px;
      line-height: 28px;
      text-indent: 2em;
      margin-top: 6px;
      word-break: break-all;
    }
    .cmt-footer {
      font-size: 12px;
      color: gray;
      margin-top: 10px;
    }
  }
}
/*美化 - 文章详情 - 底部的评论页面 */
// 外层容器
.art-cmt-container-1 {
  padding-bottom: 46px;
}
.art-cmt-container-2 {
  padding-bottom: 80px;
}

// 发布评论的盒子 - 1
.add-cmt-box {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  box-sizing: border-box;
  background-color: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 46px;
  line-height: 46px;
  padding-left: 10px;
  .ipt-cmt-div {
    flex: 1;
    border: 1px solid #efefef;
    border-radius: 15px;
    height: 30px;
    font-size: 12px;
    line-height: 30px;
    padding-left: 15px;
    margin-left: 10px;
    background-color: #f8f8f8;
  }
  .icon-box {
    width: 40%;
    display: flex;
    justify-content: space-evenly;
    line-height: 0;
  }
}

.child {
  width: 20px;
  height: 20px;
  background: #f2f3f5;
}

// 发布评论的盒子 - 2
.cmt-box {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 80px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  padding-left: 10px;
  box-sizing: border-box;
  background-color: white;
  textarea {
    flex: 1;
    height: 66%;
    border: 1px solid #efefef;
    background-color: #f8f8f8;
    resize: none;
    border-radius: 6px;
    padding: 5px;
  }
  .van-button {
    height: 100%;
    border: none;
  }
}
</style>
